package com.ys.libaop;

import android.util.Log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

@Aspect
public class DemoAspect {

    private static final String TAG = DemoAspect.class.getSimpleName();

	// 具体使用的时候，CustomPointCut 要改为具体的全限定名.
    @Pointcut("execution(@com.ys.libaop.CustomPointCut * *..*.*(..))")
    public void customMethod() {
    }
    
    @Around("customMethod()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {

        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        // 类名
        String className = methodSignature.getDeclaringType().getSimpleName();
        // 方法名
        String methodName = methodSignature.getName();
        // 功能名

        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            Log.e(TAG,"参数:"+arg.toString());
        }
        CustomPointCut behaviorTrace = methodSignature.getMethod().getAnnotation(CustomPointCut.class);
        String[] value = behaviorTrace.permissionName();
        // value -- > phone,status
        for (String s : value) {
            Log.e(TAG,"权限:"+s);
        }
        long start = System.currentTimeMillis();

        // 也可以不执行joinPoint.proceed()，根据业务需求没有权限/登录不调用目标方法
        Object result = joinPoint.proceed();// result 为目标方法调用后的返回值
        
        long duration = System.currentTimeMillis() - start;//可以统计方法耗时.

        return result;
    }
}
